/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-11-26
*/
package com.rzico.account.service;

import com.rzico.account.entity.FoundationFlow;
import com.rzico.account.mapper.FoundationFlowMapper;
import com.rzico.base.BaseMapper;
import com.rzico.base.CommResult;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.account.entity.Foundation;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Vip;
import com.rzico.basics.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.account.mapper.FoundationMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 基金管理业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class FoundationService extends BaseServiceImpl<Foundation, String> {

    @Autowired
    private FoundationMapper foundationMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private PointService pointService;

    @Autowired
    private FoundationFlowMapper foundationFlowMapper;

    @Override
    public BaseMapper<Foundation, String> getMapper() {
        return foundationMapper;
    }

    public Foundation getInfo(Long enterpriseId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("enterpriseId", enterpriseId);

        Foundation foundation = null;
        List<Foundation> list = foundationMapper.selectList(params);
        if (list.size()>0) {
            foundation = list.get(0);
        } else {
            foundation = new Foundation();
            foundation.setEnterpriseId(enterpriseId);
            foundation.setAtvy(BigDecimal.ZERO);
            foundation.setAtvyRate(BigDecimal.ZERO);
            foundation.setBonus(BigDecimal.ZERO);
            foundation.setBonusRate(BigDecimal.ZERO);
            foundation.setFund(BigDecimal.ZERO);
            foundation.setFundRate(BigDecimal.ZERO);
            foundation.setVip1(BigDecimal.ZERO);
            foundation.setVip2(BigDecimal.ZERO);
            foundation.setVip3(BigDecimal.ZERO);
            foundation.setVip4(BigDecimal.ZERO);
            foundation.setVip5(BigDecimal.ZERO);
            foundation.setCreateDate(new Date());
            foundationMapper.insertUseGeneratedKeys(foundation);
        }

        return  foundation;
    }


    @Transactional(rollbackFor = Exception.class)
    public void addFoundation(Long id,Long memberId,BigDecimal vip1,BigDecimal vip2,BigDecimal vip3,BigDecimal vip4,BigDecimal vip5,BigDecimal fund,BigDecimal atvy,BigDecimal bonus) {

        Map<String,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("vip1",vip1);
        params.put("vip2",vip2);
        params.put("vip3",vip3);
        params.put("vip4",vip4);
        params.put("vip5",vip5);
        params.put("fund",fund);
        params.put("atvy",atvy);
        params.put("bonus",bonus);
        foundationMapper.addFoundation(params);

        Foundation foundation = foundationMapper.selectByPrimaryKey(id);

        Member member = memberMapper.selectByPrimaryKey(memberId);

        if (vip1.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(vip1);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getVip1());
            flow.setFlowType(1);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }


        if (vip2.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(vip2);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getVip2());
            flow.setFlowType(2);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }



        if (vip3.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(vip3);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getVip3());
            flow.setFlowType(3);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }



        if (vip4.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(vip4);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getVip4());
            flow.setFlowType(4);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }



        if (vip5.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(vip5);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getVip5());
            flow.setFlowType(5);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }


        if (fund.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(fund);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getFund());
            flow.setFlowType(6);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }



        if (atvy.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(atvy);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getAtvy());
            flow.setFlowType(7);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }



        if (bonus.compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(bonus);
            flow.setDebit(BigDecimal.ZERO);
            flow.setBalance(foundation.getBonus());
            flow.setFlowType(8);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(memberId);
            flow.setMemo(member.getName()+"贡献的收入");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    public void jobTask(Vip vip) {

        Map<String,Object> params = new HashMap<>();
        params.put("vip",vip.getVip());
        params.put("enterpriseId",vip.getEnterpriseId());
        Foundation foundation = getInfo(vip.getEnterpriseId());

        if (vip.getVip()==1 && foundation.getVip1().compareTo(BigDecimal.ZERO)<=0) {
            return;
        }
        if (vip.getVip()==2 && foundation.getVip2().compareTo(BigDecimal.ZERO)<=0) {
            return;
        }
        if (vip.getVip()==3 && foundation.getVip3().compareTo(BigDecimal.ZERO)<=0) {
            return;
        }
        if (vip.getVip()==4 && foundation.getVip4().compareTo(BigDecimal.ZERO)<=0) {
            return;
        }
        if (vip.getVip()==5 && foundation.getVip5().compareTo(BigDecimal.ZERO)<=0) {
            return;
        }

        List<Member> memberList = memberMapper.selectList(params);
        if (memberList.size()>0) {
            BigDecimal price = BigDecimal.ZERO;
            BigDecimal balance = BigDecimal.ZERO;
            if (vip.getVip().compareTo(1)==0) {
                balance = foundation.getVip1();
                price = foundation.getVip1().divide(new BigDecimal(memberList.size()),0,BigDecimal.ROUND_DOWN);
                foundation.setVip1(foundation.getVip1().subtract(price.multiply(new BigDecimal(memberList.size()))));
                foundationMapper.updateByPrimaryKeySelective(foundation);
                for (Member member : memberList) {
                    pointService.addPoint(member.getId(),price.longValue(),vip.getName()+"-分红收益",0,null);
                    balance = balance.subtract(price);
                    FoundationFlow flow = new FoundationFlow();
                    flow.setType(0);
                    flow.setCredit(BigDecimal.ZERO);
                    flow.setDebit(price);
                    flow.setBalance(balance);
                    flow.setFlowType(1);
                    flow.setCreateDate(new Date());
                    flow.setEnterpriseId(foundation.getEnterpriseId());
                    flow.setMemberId(member.getId());
                    flow.setMemo(vip.getName()+"-分红支出");
                    foundationFlowMapper.insertUseGeneratedKeys(flow);
                }
            }
            if (vip.getVip().compareTo(2)==0) {
                balance = foundation.getVip2();
                price = foundation.getVip2().divide(new BigDecimal(memberList.size()),0,BigDecimal.ROUND_DOWN);
                foundation.setVip2(foundation.getVip2().subtract(price.multiply(new BigDecimal(memberList.size()))));
                foundationMapper.updateByPrimaryKeySelective(foundation);
                for (Member member : memberList) {
                    pointService.addPoint(member.getId(),price.longValue(),vip.getName()+"-分红收益",0,null);
                    balance = balance.subtract(price);
                    FoundationFlow flow = new FoundationFlow();
                    flow.setType(0);
                    flow.setCredit(BigDecimal.ZERO);
                    flow.setDebit(price);
                    flow.setBalance(balance);
                    flow.setFlowType(2);
                    flow.setCreateDate(new Date());
                    flow.setEnterpriseId(foundation.getEnterpriseId());
                    flow.setMemberId(member.getId());
                    flow.setMemo(vip.getName()+"-分红支出");
                    foundationFlowMapper.insertUseGeneratedKeys(flow);
                }
            }
            if (vip.getVip().compareTo(3)==0) {
                balance = foundation.getVip3();
                price = foundation.getVip3().divide(new BigDecimal(memberList.size()),0,BigDecimal.ROUND_DOWN);
                foundation.setVip3(foundation.getVip3().subtract(price.multiply(new BigDecimal(memberList.size()))));
                foundationMapper.updateByPrimaryKeySelective(foundation);
                for (Member member : memberList) {
                    pointService.addPoint(member.getId(),price.longValue(),vip.getName()+"-分红收益",0,null);
                    balance = balance.subtract(price);
                    FoundationFlow flow = new FoundationFlow();
                    flow.setType(0);
                    flow.setCredit(BigDecimal.ZERO);
                    flow.setDebit(price);
                    flow.setBalance(balance);
                    flow.setFlowType(3);
                    flow.setCreateDate(new Date());
                    flow.setEnterpriseId(foundation.getEnterpriseId());
                    flow.setMemberId(member.getId());
                    flow.setMemo(vip.getName()+"-分红支出");
                    foundationFlowMapper.insertUseGeneratedKeys(flow);
                }
            }
            if (vip.getVip().compareTo(4)==0) {
                balance = foundation.getVip4();
                price = foundation.getVip4().divide(new BigDecimal(memberList.size()),0,BigDecimal.ROUND_DOWN);
                foundation.setVip4(foundation.getVip4().subtract(price.multiply(new BigDecimal(memberList.size()))));
                foundationMapper.updateByPrimaryKeySelective(foundation);
                for (Member member : memberList) {
                    pointService.addPoint(member.getId(),price.longValue(),vip.getName()+"-分红收益",0,null);
                    balance = balance.subtract(price);
                    FoundationFlow flow = new FoundationFlow();
                    flow.setType(0);
                    flow.setCredit(BigDecimal.ZERO);
                    flow.setDebit(price);
                    flow.setBalance(balance);
                    flow.setFlowType(4);
                    flow.setCreateDate(new Date());
                    flow.setEnterpriseId(foundation.getEnterpriseId());
                    flow.setMemberId(member.getId());
                    flow.setMemo(vip.getName()+"-分红支出");
                    foundationFlowMapper.insertUseGeneratedKeys(flow);
                }
            }
            if (vip.getVip().compareTo(5)==0) {
                balance = foundation.getVip5();
                price = foundation.getVip5().divide(new BigDecimal(memberList.size()),0,BigDecimal.ROUND_DOWN);
                foundation.setVip5(foundation.getVip5().subtract(price.multiply(new BigDecimal(memberList.size()))));
                foundationMapper.updateByPrimaryKeySelective(foundation);
                for (Member member : memberList) {
                    pointService.addPoint(member.getId(),price.longValue(),vip.getName()+"-分红收益",0,null);
                    balance = balance.subtract(price);
                    FoundationFlow flow = new FoundationFlow();
                    flow.setType(0);
                    flow.setCredit(BigDecimal.ZERO);
                    flow.setDebit(price);
                    flow.setBalance(balance);
                    flow.setFlowType(5);
                    flow.setCreateDate(new Date());
                    flow.setEnterpriseId(foundation.getEnterpriseId());
                    flow.setMemberId(member.getId());
                    flow.setMemo(vip.getName()+"-分红支出");
                    foundationFlowMapper.insertUseGeneratedKeys(flow);
                }
            }
        }
        if (vip.getVip().compareTo(1)==0 && foundation.getVip1().compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(BigDecimal.ZERO);
            flow.setDebit(foundation.getVip1());
            flow.setBalance(BigDecimal.ZERO);
            flow.setFlowType(5);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(null);
            flow.setMemo("基金清每天清零");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
            foundation.setVip1(BigDecimal.ZERO);
            foundationMapper.updateByPrimaryKeySelective(foundation);
        }
        if (vip.getVip().compareTo(2)==0 && foundation.getVip2().compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(BigDecimal.ZERO);
            flow.setDebit(foundation.getVip2());
            flow.setBalance(BigDecimal.ZERO);
            flow.setFlowType(5);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(null);
            flow.setMemo("基金清每天清零");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
            foundation.setVip2(BigDecimal.ZERO);
            foundationMapper.updateByPrimaryKeySelective(foundation);
        }
        if (vip.getVip().compareTo(3)==0 && foundation.getVip3().compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(BigDecimal.ZERO);
            flow.setDebit(foundation.getVip3());
            flow.setBalance(BigDecimal.ZERO);
            flow.setFlowType(5);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(null);
            flow.setMemo("基金清每天清零");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
            foundation.setVip3(BigDecimal.ZERO);
            foundationMapper.updateByPrimaryKeySelective(foundation);
        }
        if (vip.getVip().compareTo(4)==0 && foundation.getVip4().compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(BigDecimal.ZERO);
            flow.setDebit(foundation.getVip4());
            flow.setBalance(BigDecimal.ZERO);
            flow.setFlowType(5);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(null);
            flow.setMemo("基金清每天清零");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
            foundation.setVip4(BigDecimal.ZERO);
            foundationMapper.updateByPrimaryKeySelective(foundation);
        }
        if (vip.getVip().compareTo(5)==0 && foundation.getVip5().compareTo(BigDecimal.ZERO)>0) {
            FoundationFlow flow = new FoundationFlow();
            flow.setType(0);
            flow.setCredit(BigDecimal.ZERO);
            flow.setDebit(foundation.getVip5());
            flow.setBalance(BigDecimal.ZERO);
            flow.setFlowType(5);
            flow.setCreateDate(new Date());
            flow.setEnterpriseId(foundation.getEnterpriseId());
            flow.setMemberId(null);
            flow.setMemo("基金清每天清零");
            foundationFlowMapper.insertUseGeneratedKeys(flow);
            foundation.setVip5(BigDecimal.ZERO);
            foundationMapper.updateByPrimaryKeySelective(foundation);
        }
    }


}
